package com.youbug.mall.order_oms.web;

import com.alipay.api.AlipayApiException;
import com.youbug.common.config.AlipayTemplate;
import com.youbug.common.vo.PayVo;
import com.youbug.mall.order_oms.entity.OmsOrder;
import com.youbug.mall.order_oms.service.IOmsOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Slf4j
@Controller
@RequestMapping("/mall/order")
public class PayWebController {

    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private IOmsOrderService orderService;

    /**
     * 用户下单:支付宝支付
     * 1、让支付页让浏览器展示
     * 2、支付成功以后，跳转到用户的订单列表页
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @ResponseBody
    @GetMapping(value = "/aliPayOrder", produces = "text/html")
    public String aliPayOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {

        PayVo payVo = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);
        return pay;
    }


    //根据订单号查询订单状态的API
    @GetMapping(value = "/queryByOrderSn")
    @ResponseBody
    public OmsOrder queryByOrderId(@RequestParam("OrderSn") String OrderSn) {
        log.info("查询支付记录...");
        return orderService.getOrderByOrderSn(OrderSn);
    }



}
